154850
@@ -318,9 +318,9 @@
private CreateTableDesc getBaseCreateTableDescFromTable(String dbName,
     return tblDesc;
   }
 
-  private Task<?> loadTable(URI fromURI, Table table, boolean replace) {
+  private Task<?> loadTable(URI fromURI, Table table, boolean replace, Path tgtPath) {
     Path dataPath = new Path(fromURI.toString(), "data");
-    Path tmpPath = ctx.getExternalTmpPath(new Path(fromURI));
+    Path tmpPath = ctx.getExternalTmpPath(tgtPath);
     Task<?> copyTask = TaskFactory.get(new CopyWork(dataPath,
        tmpPath, false), conf);
     LoadTableDesc loadTableWork = new LoadTableDesc(tmpPath,
@@ -390,7 +390,8 @@
private CreateTableDesc getBaseCreateTableDescFromTable(String dbName,
       LOG.debug("adding dependent CopyWork/AddPart/MoveWork for partition "
           + partSpecToString(partSpec.getPartSpec())
           + " with source location: " + srcLocation);
-      Path tmpPath = ctx.getExternalTmpPath(new Path(fromURI));
+      Path tgtLocation = new Path(partSpec.getLocation());
+      Path tmpPath = ctx.getExternalTmpPath(tgtLocation);
       Task<?> copyTask = TaskFactory.get(new CopyWork(new Path(srcLocation),
           tmpPath, false), conf);
       Task<?> addPartTask = TaskFactory.get(new DDLWork(getInputs(),
@@ -431,7 +432,8 @@
private void fixLocationInPartSpec(
       tgtPath = new Path(tblDesc.getLocation(),
           Warehouse.makePartPath(partSpec.getPartSpec()));
     }
-    checkTargetLocationEmpty(fs, tgtPath, replicationSpec);
+    FileSystem tgtFs = FileSystem.get(tgtPath.toUri(), conf);
+    checkTargetLocationEmpty(tgtFs, tgtPath, replicationSpec);
     partSpec.setLocation(tgtPath.toString());
   }
 
@@ -707,8 +709,10 @@
private void createRegularImportTasks(
       } else {
         LOG.debug("table non-partitioned");
         // ensure if destination is not empty only for regular import
-        checkTargetLocationEmpty(fs, new Path(table.getDataLocation().toString()), replicationSpec);
-        loadTable(fromURI, table, false);
+        Path tgtPath = new Path(table.getDataLocation().toString());
+        FileSystem tgtFs = FileSystem.get(tgtPath.toUri(), conf);
+        checkTargetLocationEmpty(tgtFs, tgtPath, replicationSpec);
+        loadTable(fromURI, table, false, tgtPath);
       }
       // Set this to read because we can't overwrite any existing partitions
       outputs.add(new WriteEntity(table, WriteEntity.WriteType.DDL_NO_LOCK));
@@ -741,8 +745,9 @@
private void createRegularImportTasks(
           } else {
             tablePath = wh.getTablePath(parentDb, tblDesc.getTableName());
           }
-          checkTargetLocationEmpty(fs, tablePath, replicationSpec);
-          t.addDependentTask(loadTable(fromURI, table, false));
+          FileSystem tgtFs = FileSystem.get(tablePath.toUri(), conf);
+          checkTargetLocationEmpty(tgtFs, tablePath, replicationSpec);
+          t.addDependentTask(loadTable(fromURI, table, false, tablePath));
         }
       }
       rootTasks.add(t);
@@ -813,7 +818,7 @@
private void createReplImportTasks(
           }
         } else {
           LOG.debug("adding dependent CopyWork/MoveWork for table");
-          t.addDependentTask(loadTable(fromURI, table, true));
+          t.addDependentTask(loadTable(fromURI, table, true, new Path(tblDesc.getLocation())));
         }
       }
       if (dr == null){
@@ -868,7 +873,7 @@
private void createReplImportTasks(
           return; // silently return, table is newer than our replacement.
         }
         if (!replicationSpec.isMetadataOnly()) {
-          loadTable(fromURI, table, true); // repl-imports are replace-into
+          loadTable(fromURI, table, true, new Path(fromURI)); // repl-imports are replace-into
         } else {
           rootTasks.add(alterTableTask(tblDesc));
         }
